package com.wu.mvp.app.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.wu.mvp.app.bijection.BeamAppCompatActivity;
import com.wu.mvp.app.bijection.Presenter;


public class BaseActivity<T extends Presenter> extends BeamAppCompatActivity<T> {

    /**
     * 视图结构
     * DecorView
     * |
     * LinearLayout
     * /         \
     * /           \
     * FrameLayout       FrameLayout
     * |             (mContentParent)
     * |                   /      \
     * TextView     FrameLayout    各种附加View
     * (mContent)
     * (在一开始就装入View树)
     * |
     * setContent()
     * (什么时候装入View不确定)
     */
    private FrameLayout mContent;
    private FrameLayout mContentParent;
    private boolean isUnFinish;

    @Override
    public void preCreatePresenter() {
        super.preCreatePresenter();
        initViewTree();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void initViewTree() {
        mContentParent = (FrameLayout) findViewById(android.R.id.content);
        mContent = new FrameLayout(this);
        super.setContentView(mContent);
    }

    public FrameLayout getParentView() {
        return mContentParent;
    }

    @Override
    public void setContentView(int layoutResID) {
        this.setContentView(getLayoutInflater().inflate(layoutResID, mContent, false));
    }

    @Override
    public void setContentView(View view) {
        this.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContent.addView(view, params);
    }


    public void onSetToolbar(Toolbar toolbar) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home && !isUnFinish) {
            finishActivity();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Nullable
    protected final <E extends View> E $(@NonNull View view, @IdRes int id) {
        return (E) view.findViewById(id);
    }

    @Nullable
    protected final <E extends View> E viewId(@NonNull View view, @IdRes int id) {
        return (E) view.findViewById(id);
    }

    @Nullable
    protected final <E extends View> E $(@IdRes int id) {
        return (E) findViewById(id);
    }

    @Nullable
    protected final <E extends View> E viewId(@IdRes int id) {
        return (E) findViewById(id);
    }


    public void finishActivity() {
    }

    public void setUnFinish(boolean unFinish) {
        isUnFinish = unFinish;
    }
}

